package com.mobilekites.gcm;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class GCMController {
	private static final String TAG = "GCMController";

	private static final int TYPE_DATA = 100;
	private static final int TYPE_UPDATE = 200;
	private static final int TYPE_ADS = 300;

	private JSONObject mJsonObject;
	private Context mContext;

	public GCMController(Context context, String data) {
		try {
			mJsonObject = new JSONObject(data);
			mContext = context;

		} catch (JSONException e) {
			e.printStackTrace();
			Log.e(TAG, "Khong phan tich duoc du lieu tra ve, data=[" + data
					+ "]");
		}
	}

	public void execute() {
		try {
			int code = mJsonObject.getInt("code");
			Log.i(TAG, "Code=" + code);
			switch (code) {
			case TYPE_DATA:

				// TODO Khong xu ly update data vi data qua' lon' khong gui duoc
				// qua GCM

				// JSONObject jsonObject=mJsonObject.getJSONObject("data");
				// DataUpdateController dataUpdateController=new
				// DataUpdateController(mContext, jsonObject);
				// dataUpdateController.execute();
				break;
			case TYPE_UPDATE:
				UpdateController controller = new UpdateController(mContext,
						mJsonObject);
				controller.saveInfo();
				break;
			case TYPE_ADS:
				AdsController adsController = new AdsController(mContext,
						mJsonObject);
				adsController.execute();
				break;

			default:
				break;
			}

		} catch (JSONException e) {
			e.printStackTrace();
			Log.e(TAG, "Execute error parse JSON");
		}
	}

}
